package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *东南网解析
 *http://mil.fjsen.com/ 福建国防教育网
 * @author Persh
 *
 */
public class FjsenNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.fjsen.com/.*[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm",
		"http://pp.fjsen.com/news/.*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]*.html",
		"http://tea.fjsen.com/view/[0-9]{4}-[0-9]{2}-[0-9]{2}/show[0-9]*.html",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		if(url.startsWith("http://pp.fjsen.com/")){
			title=doc.select("div.title_info h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.newstext"));
			String text=doc.select("div.info_text").text();
			date=DateUtils.matchDate(text);
			author= StringUtils.substringBetween(text, "来源：","　");
		}else if(url.startsWith("http://tea.fjsen.com/")){
			title=doc.select("div.title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.news_content"));
			String text=doc.select("html >body >div.conter >div.left> div.left_content> div:eq(1)").text();
			date=DateUtils.matchDate(text);
			author= doc.select("html >body >div.conter >div.left> div.left_content> div:eq(1)> a").text();
		}else{
			title=doc.select("div.cont_head h1").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("#new_subject_id").text();
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("div.cont-news"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.cont-news"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("#new_message_id"));
				}
			}
			date=DateUtils.matchDate(doc.select("td.zb").text());
			if(date==null){
				date =JSoupUtils.matchDate(doc, "来源：");
			}
			author=doc.select("td.zb a").text().replace("我来说两句", "").trim();
		}
		author=JSoupUtils.matchAuthor(doc,  "来源：");
		news.setAuthor(author);
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		FjsenNewsAnalyse analyse=new FjsenNewsAnalyse();
		String url="http://taihai.fjsen.com/2014-08/14/content_14681782.htm";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
